/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.helper;

import java.util.ArrayList;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.GraphEdgeHelper;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * 
 */
public class EdgeElementImportHelper extends GraphEdgeHelper {

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.IGraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode, com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */	
	/**
	 * 
	 * 
	 * @param target 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canConnect(Element source,Element target){
		if(source!=null &&(source instanceof Package)&& target==null){
			return true;}

		if ((source instanceof Package) && (target instanceof PackageableElement)){// they are  classifiers
			if (source==target){return false;}// avoid self import
			return true; // attention Detection of loops
		}
		return false;

	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement source){
		Element eltSource=null;
		if ((source instanceof Diagram)  ){
			if (((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement();
				if (eltSource instanceof Package){return true;}
			}
		}
		if (source.getSemanticModel()!=null ){
			if (((Uml1SemanticModelBridge)source.getSemanticModel()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)source.getSemanticModel()).getElement();
				if (eltSource instanceof Package){// they are  classifiers
					return true;
				}
			}
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#createGraphEdge(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
	/**
	 * 
	 * 
	 * @param umlElement 
	 * @param diagram 
	 * 
	 * @return 
	 */
	@Override
	public ArrayList createGraphEdge(Element umlElement, Diagram diagram){
		Debug.debug(this, "createGraphEdge", 10);

		GraphEdge ge = (GraphEdge)this.createGraphElement(umlElement, null,null,null);		
		//look for UML connected UML element
		if (!(umlElement instanceof ElementImport)) {
			Debug.error(this,
					"EdgeElementImportFactory.createGraphEdge: " + umlElement + "is not an ElementImport!",
					channel);
		}

		ElementImport de = (ElementImport) umlElement;

		Element umlsource = de.getImportingNamespace();
		Element umltarget = de.getImportedElement();
		super.endCreateGraphElement(umlsource, umltarget, ge, diagram);
		return new ArrayList();
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createGraphNodeWithSemantic(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlChild 
	 * @param size 
	 * @param location 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public GraphElement createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size) {

		GraphElement ge=super.createGraphElement(umlChild, null, location, size);
		NodeHelper nf=new NodeHelper();
//		stereotype of the Generalization
		GraphNode graphNode=nf.createGraphElement(null,ge,new Point(0, 30),null);
		graphNode.setFontSize(9);

//		the parent is connected in last, because an eventis triggered.
		if( parent!=null){
			parent.getContained().add(ge);
		}
		return ge;	
	}

	///////////////////////////////////////////////////////////////////////////
	// UML Management
	///////////////////////////////////////////////////////////////////////////

	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	public org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"create ElementImport", channel); //$NON-NLS-1$
		ElementImport ei = UMLFactory.eINSTANCE.createElementImport();
		((Package)umlParent).getElementImports().add(ei);
		return ei;		
	}


	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof Package) {
			return true;
		}
		return false;
	}


	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean addUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getElementImports().add((ElementImport)element);
		}
		return false;
	}


	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	public boolean removeUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getElementImports().remove(element);
		}
		return false;
	}

	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	public boolean connect(Element source, Element target, Element link) {
		ElementImport gen =(ElementImport)link;
		// this is the getSource() that contains the Generalization
		Package cls= (Package)source;
		gen.setImportedElement((PackageableElement)target);
		cls.getElementImports().add(gen);
		return true;
	}
	
	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	public boolean deconnect(Element source, Element target, Element link) {
		ElementImport gen =(ElementImport)link;
		// this is the getSource() that contains the Generalization
		Package cls= (Package)source;
		gen.setImportedElement(null);
		cls.getElementImports().remove(gen);
		return true;
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	public String getBaseString() {
		return "ElementImport"; //$NON-NLS-1$
	}
}


